package com.common.base.utils;

import android.app.Activity;
import android.graphics.Rect;
import android.view.View;
import android.view.ViewTreeObserver;
import android.widget.FrameLayout;

import java.lang.ref.WeakReference;

public class AndroidBug5497Workaround {

    // For icon_base_more_white information, see https://code.google.com/p/android/issues/detail?id=5497
    // To use this class, simply invoke assistActivity() on an Activity that already has its content view set.

    public static AndroidBug5497Workaround assistActivity(Activity activity) {
        return new AndroidBug5497Workaround(activity);
    }

    public void removeActivity() {
        if (mChildOfContent != null && onGlobalLayoutListener != null) {
            possiblyResizeChildOfContent();
            mChildOfContent.getViewTreeObserver().removeOnGlobalLayoutListener(onGlobalLayoutListener);
        }
    }

    private final View mChildOfContent;
    private int usableHeightPrevious;
    private FrameLayout.LayoutParams frameLayoutParams;
    private WeakReference<Activity> activity;
    private ViewTreeObserver.OnGlobalLayoutListener onGlobalLayoutListener;
    private boolean isSoftKeyboardOpened = false;

    private AndroidBug5497Workaround(Activity activity) {
        this.activity = new WeakReference<>(activity);
        FrameLayout content = activity.findViewById(android.R.id.content);
        mChildOfContent = content.getChildAt(0);
        if (mChildOfContent != null) {
            onGlobalLayoutListener = new ViewTreeObserver.OnGlobalLayoutListener() {
                @Override
                public void onGlobalLayout() {
                    possiblyResizeChildOfContent();
                }
            };
            mChildOfContent.getViewTreeObserver().addOnGlobalLayoutListener(onGlobalLayoutListener);
//        mChildOfContent.getViewTreeObserver().addOnGlobalLayoutListener(this::possiblyResizeChildOfContent);
            frameLayoutParams = (FrameLayout.LayoutParams) mChildOfContent.getLayoutParams();
        }
    }

    private void possiblyResizeChildOfContent() {
        if (activity == null || activity.get() == null) {
            return;
        }

        if (mChildOfContent == null || frameLayoutParams == null) {
            return;
        }
        int usableHeightNow = computeUsableHeight();
        if (usableHeightNow != usableHeightPrevious) {
            int statusHeight = ViewUtils.INSTANCE.getNavigationBarHeight(activity.get().getWindow());
            int usableHeightSansKeyboard = mChildOfContent.getRootView().getHeight() - statusHeight;
            int heightDifference = usableHeightSansKeyboard - usableHeightNow;
            if (heightDifference > (usableHeightSansKeyboard / 4)) {
                // keyboard probably just became visible
                isSoftKeyboardOpened = true;
                frameLayoutParams.height = usableHeightSansKeyboard - heightDifference;
            } else {
                // keyboard probably just became hidden
                isSoftKeyboardOpened = false;
                frameLayoutParams.height = usableHeightSansKeyboard;
            }
            mChildOfContent.requestLayout();
            usableHeightPrevious = usableHeightNow;
        }
    }

    private int computeUsableHeight() {
        Rect r = new Rect();
        mChildOfContent.getWindowVisibleDisplayFrame(r);
        return (r.bottom - r.top + ViewUtils.INSTANCE.getStatusHeight());// 全屏模式下： return r.bottom
    }

    public boolean isOpen() {
        return isSoftKeyboardOpened;
    }

    public void release(){
        if (activity != null) {
            activity.clear();
            activity = null;
        }
    }
}